package com.likui.myqq.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.likui.myqq.R;
import com.likui.myqq.fragment.NewsFatherFragment;

/**
 * Created by helikui on 2015/9/28.
 */
public class MainActivity extends FragmentActivity {
    private static final String TAG = "MainActivity";
    private Context mContext;

    private ImageButton mNews, mContact, mDynamic, mSetting;
    private View mPopView;
    private View currentButton;

    private TextView app_cancel;
    private TextView app_exit;
    private TextView app_change;

    private int mLevel = 1;
    private PopupWindow mPopupWindow;
    private LinearLayout bottomBarGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        findView();
        init();
    }

    private void init() {
        mNews.setOnClickListener(newsClickListener);
        mContact.setOnClickListener(contactClickListener);
        mDynamic.setOnClickListener(dynamicClickListener);
        mSetting.setOnClickListener(settingClickListener);

        mNews.performClick();

        mPopupWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        app_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        app_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.activity_up, R.anim.fade_out);
                finish();
            }
        });

        app_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void findView() {
        mPopView = LayoutInflater.from(mContext).inflate(R.layout.app_exit, null);
        bottomBarGroup = (LinearLayout) findViewById(R.id.bottom_bar_group);
        mNews = (ImageButton) findViewById(R.id.bottom_news);
        mContact = (ImageButton) findViewById(R.id.bottom_contact);
        mDynamic = (ImageButton) findViewById(R.id.bottom_dynamic);
        mSetting = (ImageButton) findViewById(R.id.bottom_setting);

        app_cancel = (TextView) mPopView.findViewById(R.id.app_cancel);
        app_change = (TextView) mPopView.findViewById(R.id.app_change_user);
        app_exit = (TextView) mPopView.findViewById(R.id.app_exit);
    }

    private View.OnClickListener newsClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            NewsFatherFragment newsFatherFragment = new NewsFatherFragment();
            ft.replace(R.id.fl_content, newsFatherFragment, TAG);
            ft.commit();
            setButton(v);
        }
    };


    private View.OnClickListener contactClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setButton(v);
        }
    };
    private View.OnClickListener dynamicClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setButton(v);
        }
    };
    private View.OnClickListener settingClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setButton(v);
        }
    };

    private void setButton(View v) {
        if (currentButton != null && currentButton.getId() != v.getId()) {
            currentButton.setEnabled(true);
        }
        v.setEnabled(false);
        currentButton = v;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            mPopupWindow.setBackgroundDrawable(new ColorDrawable((Color.parseColor("#b0000000"))));
            mPopupWindow.showAtLocation(bottomBarGroup, Gravity.BOTTOM, 0, 0);
            mPopupWindow.setAnimationStyle(R.style.app_pop);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setFocusable(true);
            mPopupWindow.update();
        }
        return super.onKeyDown(keyCode, event);
    }
}
